package com.rtu.web;

import com.rtu.dto.InsertOrUpImageDto;
import com.rtu.dto.QueryImageReqDto;
import com.rtu.dto.response.ApiResponse;
import com.rtu.enums.Code;
import com.rtu.service.IImageService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by jiafeilu on 2018/5/7.
 */
@Controller
@RequestMapping("/image")
public class ImageController {
    private static final Logger logger = LoggerFactory.getLogger(ImageController.class);

    @Autowired
    private IImageService imageService;

    @RequestMapping("/addOrUpdate")
    @ResponseBody
    public ApiResponse addOrUpdateImage(InsertOrUpImageDto dto){
        ApiResponse apiResponse;
        try {
            if (dto == null || dto.getAreaId() == null || dto.getCh() == null || StringUtils.isBlank(dto.getImageUrl())) {
                apiResponse = ApiResponse.create(false,null, Code.BAD_PARAMS.code,null);
                return apiResponse;
            }
            imageService.insertOrUpdate(dto);
            apiResponse = ApiResponse.create(true, Code.SUCCESS,null);
        }catch (Exception e){
            logger.error("getList",e);
            apiResponse = ApiResponse.create(false,e.getMessage(), Code.COMMON_SERVER_ERROR.code,null);
        }
        return apiResponse;
    }


    @RequestMapping("/query")
    @ResponseBody
    public ApiResponse query(QueryImageReqDto dto){
        ApiResponse apiResponse;
        try {
            if (dto == null || dto.getAreaId() == null || dto.getCh() == null ) {
                apiResponse = ApiResponse.create(false,null, Code.BAD_PARAMS.code,null);
                return apiResponse;
            }
            apiResponse = ApiResponse.create(true, Code.SUCCESS,imageService.query(dto));
        }catch (Exception e){
            logger.error("query",e);
            apiResponse = ApiResponse.create(false,e.getMessage(), Code.COMMON_SERVER_ERROR.code,null);
        }
        return apiResponse;
    }
}
